package org.example;

public class test9 {
    public static void main(String[] args) {
        Student[] arr = new Student[3];
        Student s1 = new Student("张三", 20, 90);
        Student s2 = new Student("李四", 21, 91);
        Student s3 = new Student("王五", 22, 92);
        arr[0] = s1;
        arr[1] = s2;
        arr[2] = s3;

        int Index = getIndex(arr, 91);
        System.out.println(Index);
        if (Index >= 0) {
            int newAge = arr[Index].getAge() + 1;
            //修改学生年龄
            arr[Index].setAge(newAge);
            //获取学生信息
            System.out.println(arr[Index].getName() + "," + arr[Index].getAge() + "," + arr[Index].getId());
            //元素存在，删除该元素
            arr[Index] = null;
            //遍历数组，打印学生信息
            printStudent(arr);
        } else {
            System.out.println("该学生不存在");
        }
        //创建一个新的学生对象
        Student s4 = new Student("赵六", 23, 93);
        //唯一性判断
        boolean flag = contains(arr, s4.getId());
        //遍历数组，判断是否包含指定id的学生
        if (flag) {
            //重复
            System.out.println("该学生已存在");
        } else {
            //不重复
            int count = getCount(arr);
            //判断数组是否已满
            if (count == arr.length) {
                System.out.println("数组已满");
                //创建一个新数组，将原数组中的元素复制到新数组中
                Student[] newArr = addStudent(arr);
                //将新元素添加到新数组中
                newArr[count] = s4;
                //遍历数组，打印新数组学生信息
                printStudent(newArr);
            } else {
                //数组未满,直接添加到数组中
                arr[count - 1] = s4;
                //遍历数组，打印旧数组学生信息
                printStudent(arr);
            }
        }
    }

    //遍历数组，打印学生信息
    public static void printStudent(Student[] arr) {
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if (stu != null) {
                System.out.println(stu.getName() + "," + stu.getAge() + "," + stu.getId());
            }
        }
    }

    //创建一个新数组，将原数组中的元素复制到新数组中，然后将新元素添加到新数组中
    public static Student[] addStudent(Student[] arr) {
        //创建一个新数组，长度为原数组长度+1
        Student[] newArr = new Student[arr.length + 1];
        for (int i = 0; i < arr.length; i++) {
            newArr[i] = arr[i];
        }
        return newArr;
    }

    //获取数组中元素已经的个数
    public static int getCount(Student[] arr) {
        //统计数组中有效元素个数
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if (arr[i] != null) {
                count++;
            }
        }
        return count;
    }

    //判断数组中是否包含指定id的学生
    public static boolean contains(Student[] arr, int id) {
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if (stu != null) {
                if (stu.getId() == id) {
                    return true;
                }
            }
        }
        return false;
    }

    public static int getIndex(Student[] arr, int id) {
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if (stu != null) {
                if (stu.getId() == id) {
                    return i;
                }
            }
        }
        return -1;
    }
}
